package cn.comtom.ewbs.signature.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.comtom.ewbs.signature.common.enums.BasicError;
import cn.comtom.ewbs.signature.common.vo.request.PageRequest;
import cn.comtom.ewbs.signature.common.vo.response.ApiEntityResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiPageResponse;
import cn.comtom.ewbs.signature.common.vo.response.ApiResponse;
import cn.comtom.ewbs.signature.entity.SignDev;
import cn.comtom.ewbs.signature.service.AbstractSignService;
import cn.comtom.ewbs.signature.service.ISignDevService;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuhy
 * @since 2022-11-07 11:21:28
 */
@Controller
@RequestMapping("/auth/SignDevController")
@Slf4j
@Validated
@Api(tags = "签名设备管理", description = "签名设备管理")
public class SignDevController extends AuthController {
	
	private String prefix = "admin/signDev";

	@Autowired
	private ISignDevService signDevService;
	
	/**
	 * 页面展示
	 * @param model
	 * @return String
	 * @author fuce
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	//@SaCheckPermission("gen:signDev:view")
    public String view(ModelMap model){
        return prefix + "/list";
    }
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map){
        map.put("SignDev", signDevService.getById(id));
        
        LambdaQueryWrapper<SignDev> wrapper = new LambdaQueryWrapper<SignDev>()
                .select(SignDev::getDevName);
        List<SignDev> signDevList = signDevService.list(wrapper);
        List<String> signDevNameList = signDevList.stream().map(it->it.getDevName()).collect(Collectors.toList());
        map.put("devNameList", signDevNameList);
        return prefix + "/edit";
    }
	
	/**   
     * 分页查询
     * @param signDev 请求参数 
     * @param pageReq   分页参数
     * @return ApiPageResponse 响应结果     
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ResponseBody
    public ApiPageResponse<SignDev> page(@ModelAttribute SignDev signDev, @ModelAttribute PageRequest pageReq) {
    	IPage<SignDev> pager = signDevService.selectPage(signDev,pageReq);
        return ApiPageResponse.ok(pager);
    }
    
    /**
     * 根据ID查询
     * @param id 主键
     * @return 响应结果
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    @ResponseBody
    public ApiEntityResponse<SignDev> getById(@PathVariable(name = "id") String id){
        return ApiEntityResponse.ok(signDevService.getById(id));
    }

    /**
     * 根据ID删除
     * @param id 主键
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ResponseBody
    public ApiResponse deleteById(@PathVariable(name = "id") String id){
    	if(signDevService.removeById(id)) {
    		return ApiResponse.ok();
    	}else {
    		return ApiResponse.error();
    	}
    }

    /**
     * 保存
     * @param signDev 请求数据
     * @return 响应结果
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存", notes = "保存")
    @ResponseBody
    public ApiResponse save(@Validated @RequestBody SignDev signDev){
    	if(signDevService.saveOrUpdate(signDev)) {
    		return ApiResponse.ok();
    	}else {
    		return ApiResponse.error();
    	}
    }
    
    /**
     * 更新
     * @param signDev 请求数据
     * @return 响应结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    @ResponseBody
    public ApiResponse update(@Validated @RequestBody SignDev signDev){
    	AbstractSignService signService = SpringUtil.getBean(signDev.getDevName());
    	if(signService!=null) {
    		if(signService.updateConfig(signDev.getConfilePath(), signDev.getIp(), signDev.getPort(), signDev.getSignNo(), signDev.getLogPath(), signDev.getConnectCount(), signDev.getTimeOut())) {
    			if(signDevService.saveOrUpdate(signDev)) {
    				return ApiResponse.ok();
    			}
    		}
    	}
		return ApiResponse.error();
    }
    
    /**
	 * 修改是否启用
	 * @param record
	 * @return
	 */
    @PutMapping("/updateEnable")
	@ResponseBody
    public ApiResponse updateEnable(@RequestBody SignDev record){
    	record.setUpdater(getCurrentUserInfo().getAccount());
    	record.setUpdateTime(LocalDateTimeUtil.now());
    	signDevService.updateEnable(record);
		return ApiResponse.ok();
	}

}
